<!DOCTYPE html>
<html  lang="zh-CN" >
    <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, minimum-scale=1, initial-scale=1, maximum-scale=5, viewport-fit=cover">
    <title>使用spring的requestBody实现List绑定 | 在路上</title>
    <meta name="description" content="使用spring的requestBody实现List绑定">
<meta property="og:type" content="article">
<meta property="og:title" content="使用spring的requestBody实现List绑定">
<meta property="og:url" content="http://www.lushunming.com.cn/2017/01/02/web/2017-01-2-%E4%BD%BF%E7%94%A8spring%E7%9A%84requestBody%E5%AE%9E%E7%8E%B0List%E7%BB%91%E5%AE%9A/index.html">
<meta property="og:site_name" content="在路上">
<meta property="og:description" content="使用spring的requestBody实现List绑定">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2017-01-01T16:00:00.000Z">
<meta property="article:modified_time" content="2019-11-24T07:17:53.644Z">
<meta property="article:author" content="machine">
<meta property="article:tag" content="java">
<meta name="twitter:card" content="summary">

    
    <link rel="icon" href="/images/favicon.ico" type="image/x-icon">

    
<link rel="stylesheet" href="/css/common.min.css">



    
    
    
        <link href="//cdn.jsdelivr.net/npm/gitalk@1.4.0/dist/gitalk.min.css" rel="stylesheet">
    
    
        <link href="//cdn.jsdelivr.net/npm/lightgallery.js@1.1.3/dist/css/lightgallery.min.css" rel="stylesheet">
    
    
    
<link rel="stylesheet" href="/css/iconfont.min.css">

    
<meta name="generator" content="Hexo 5.1.1"></head>

    <body>
        <header class="header header-fixture">
    <div class="profile-search-wrap flex sm:block">
        
        
        <div class="profile sm:text-center md:px-1 lg:px-3 sm:pb-4 sm:pt-6">
            <a id="avatar" role="link" href="https://www.lushunming.com.cn" class="inline-block lg:w-16 lg:h-16 w-8 h-8 m-2" target="_self" rel="noopener" rel="noreferrer" >
                <img src="/images/avatar.jpg" class="rounded-full" alt="avatar">
            </a>
            <h2 id="name" class="hidden lg:block">lushunming</h2>
            <h3 id="title" class="hidden xl:block">前码农</h3>
            
            <small id="location" class="hidden lg:block">
                <i class="iconfont icon-map-icon"></i>
                泰州
            </small>
            
        </div>
        
        
<div class="search flex-1 flex lg:inline-block sm:hidden lg:px-4 lg:mt-2 lg:mb-4 lg:w-full">
    <form id="search-form" class="my-auto flex-1 lg:border lg:border-solid lg:border-gray-200">
        <div class="input-group table bg-gray-100 lg:bg-white w-full">
            <input id="search-input" type="text" placeholder="搜索" class="inline-block w-full bg-gray-100 lg:bg-white">
            <span class="table-cell">
                <button name="search tigger button" disabled>
                    <i class="iconfont icon-search m-2"></i>
                </button>
            </span>
        </div>
    </form>
        
<script id="search-teamplate" type="text/html" data-path="/content.json">
    <div>
        <div class="search-header bg-gray-400">
            <input id="actual-search-input" model="keyword" ref="input" class="inline-block w-full h-10 px-2 py-1" placeholder="搜索" type="text">
        </div>
        <div class="search-result bg-gray-200">
            {{#each searchPosts}}
            <a href="/{{ path }}" class="result-item block px-2 pb-3 mb-1 pt-1 hover:bg-indigo-100">
                <i class="iconfont icon-file"></i>
                <h1 class="result-title inline font-medium text-lg">{{ title }}</h1>
                <p class="result-content text-gray-600 text-sm">{{{ text }}}</p>
            </a>
            {{/each}}
        </div>
    </div>
</script>

</div>


        <button name="menu toogle button" id="menu-toggle-btn" class="block sm:hidden p-3" role="button" aria-expanded="false">
            <i class="iconfont icon-hamburger"></i>
        </button>
    </div>
    <nav id="menu-nav" class="hidden sm:flex flex-col">
        
        
            <div class="menu-item menu-home" role="menuitem">
                <a href="/.">
                    <i class="iconfont icon-home" aria-hidden="true"></i>
                    <span class="menu-title">首页</span>
                </a>
            </div>
        
        
            <div class="menu-item menu-archives" role="menuitem">
                <a href="/archives">
                    <i class="iconfont icon-archive" aria-hidden="true"></i>
                    <span class="menu-title">归档</span>
                </a>
            </div>
        
        
            <div class="menu-item menu-categories" role="menuitem">
                <a href="/categories">
                    <i class="iconfont icon-folder" aria-hidden="true"></i>
                    <span class="menu-title">分类</span>
                </a>
            </div>
        
        
            <div class="menu-item menu-tags" role="menuitem">
                <a href="/tags">
                    <i class="iconfont icon-tag" aria-hidden="true"></i>
                    <span class="menu-title">标签</span>
                </a>
            </div>
        
        
            <div class="menu-item menu-repository" role="menuitem">
                <a href="/repository">
                    <i class="iconfont icon-project" aria-hidden="true"></i>
                    <span class="menu-title">项目</span>
                </a>
            </div>
        
        
            <div class="menu-item menu-links" role="menuitem">
                <a href="/links">
                    <i class="iconfont icon-friend" aria-hidden="true"></i>
                    <span class="menu-title">友链</span>
                </a>
            </div>
        
        
            <div class="menu-item menu-about" role="menuitem">
                <a href="/about">
                    <i class="iconfont icon-cup" aria-hidden="true"></i>
                    <span class="menu-title">关于</span>
                </a>
            </div>
        
        
<div class="social-links flex sm:flex-col lg:hidden mt-5">
    
        <span class="social-item text-center">
            <a target="_blank" rel="noopener" href="https://github.com/lushunming">
                <i class="iconfont social-icon icon-github"></i>
                <span class="menu-title hidden lg:inline">menu.github</span>
            </a>
        </span>
    
        <span class="social-item text-center">
            <a href="/atom.xml">
                <i class="iconfont social-icon icon-rss"></i>
                <span class="menu-title hidden lg:inline">menu.rss</span>
            </a>
        </span>
    
</div>


    </nav>
</header>

        <section class="main-section">
            
    <main class="flex-1 px-4 py-12 md:px-5 lg:px-8 lg:py-4 relative min-h-screen">
    

    <article class="content article article-archives article-type-list" itemscope="">
        <header class="article-header">
            
    
        <h1 class="article-title text-lg" itemprop="name">
            使用spring的requestBody实现List绑定
        </h1>
    



            <p class="article-meta mb-3 text-xs">
                <span class="article-date">
    <i class="iconfont icon-calendar-check"></i>
	<a href="/2017/01/02/web/2017-01-2-%E4%BD%BF%E7%94%A8spring%E7%9A%84requestBody%E5%AE%9E%E7%8E%B0List%E7%BB%91%E5%AE%9A/" class="article-date">
	  <time datetime="2017-01-01T16:00:00.000Z" itemprop="datePublished">1月 2</time>
	</a>
</span>

                
    <span class="article-category">
    <i class="iconfont icon-folder"></i>
    <a class="article-category-link" href="/categories/web/">web</a>
  </span>


                
    <span class="article-tags">
    <i class="iconfont icon-tag"></i>
    <a class="article-tag-none-link" href="/tags/java/" rel="tag">java</a>
  </span>


                <span class="_partial/post-comment"><i class="icon icon-comment"></i>
                    <a href="/2017/01/02/web/2017-01-2-%E4%BD%BF%E7%94%A8spring%E7%9A%84requestBody%E5%AE%9E%E7%8E%B0List%E7%BB%91%E5%AE%9A/#comments" class="article-comment-link">
                        评论
                    </a>
                </span>
                

            </p>
        </header>
        <div class="marked-body article-body">
            <h1>使用spring的requestBody实现List绑定</h1>
<p>最近有很多一对多关系的表单需要保存，比如一个人有好几本书，他会在一个表单提交所有的数据，我的后台参数需要绑定一个<code>List</code>。<br>
下面是人和书的model：</p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">    <span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">User</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">	<span class="comment">/** 自增型主键 */</span></span><br><span class="line">	<span class="keyword">private</span> <span class="keyword">Integer</span> id;</span><br><span class="line">	<span class="comment">/** 姓名 */</span></span><br><span class="line">	<span class="keyword">private</span> <span class="keyword">String</span> name;</span><br><span class="line">	<span class="keyword">private</span> <span class="keyword">String</span> groupId;</span><br><span class="line">	<span class="keyword">private</span> <span class="keyword">List</span>&lt;Book&gt; books;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight php"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line">   <span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Book</span> </span>&#123;</span><br><span class="line"></span><br><span class="line"><span class="comment">/** 书籍 */</span></span><br><span class="line"><span class="keyword">private</span> <span class="keyword">Integer</span> id;</span><br><span class="line"><span class="comment">/** 借书人（借书人的） */</span></span><br><span class="line"><span class="keyword">private</span> <span class="keyword">Integer</span> borrower;</span><br><span class="line"><span class="comment">/** 预订人 */</span></span><br><span class="line"><span class="keyword">private</span> <span class="keyword">Integer</span> booker;</span><br><span class="line"><span class="comment">/** 书名 */</span></span><br><span class="line"><span class="keyword">private</span> <span class="keyword">String</span> bookName;</span><br><span class="line"><span class="comment">/** 书本页数 */</span></span><br><span class="line"><span class="keyword">private</span> <span class="keyword">Integer</span> pageCount;</span><br><span class="line"><span class="comment">/** 价格 */</span></span><br><span class="line"><span class="keyword">private</span> <span class="keyword">Double</span> price;</span><br><span class="line"><span class="comment">/** 作者 */</span></span><br><span class="line"><span class="keyword">private</span> <span class="keyword">String</span> author;</span><br><span class="line"><span class="comment">/** 出版社 */</span></span><br><span class="line"><span class="keyword">private</span> <span class="keyword">String</span> press;</span><br><span class="line"><span class="comment">/** 书籍类别（对应类别表的） */</span></span><br><span class="line"><span class="keyword">private</span> <span class="keyword">Integer</span> catgory;</span><br><span class="line">   </span><br></pre></td></tr></table></figure>
<p>我的后台参数就是<code>User user</code>，其实用form提交也是可以的，只要把book的每一行设为<code>books[i].bookName</code>这样也能提交，但是公司用的是<code>EasyUI</code>的 <code>dataGrid</code>，所以name就不能由我控制。于是我查了很多资料，有了下一种方法，使用 <code>ajax</code>和<code>@RequestBody</code>的方法提交。</p>
<p>在前台，我们用<code>ajax</code></p>
<figure class="highlight routeros"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">$.ajax(&#123;</span><br><span class="line">			url : <span class="string">&#x27;/ztree/save&#x27;</span>,</span><br><span class="line">		<span class="built_in">	type </span>: <span class="string">&quot;post&quot;</span>,</span><br><span class="line">			data : JSON.stringify(&#123;</span><br><span class="line">				<span class="string">&quot;name&quot;</span> : <span class="string">&quot;a&quot;</span>,</span><br><span class="line">				<span class="string">&quot;books&quot;</span> : [&#123;<span class="string">&quot;bookName&quot;</span>:<span class="string">&quot;a&quot;</span>,<span class="string">&quot;price&quot;</span>:12.3&#125;,&#123;<span class="string">&quot;bookName&quot;</span>:<span class="string">&quot;b&quot;</span>&#125;]</span><br><span class="line">			&#125;),</span><br><span class="line">			contentType : <span class="string">&quot;application/json&quot;</span>,</span><br><span class="line">			beforeSend : function() &#123;</span><br><span class="line">				return $(<span class="string">&quot;#form&quot;</span>).valid();</span><br><span class="line">			&#125;</span><br><span class="line">		&#125;);</span><br></pre></td></tr></table></figure>
<p>我们把<code>dataGrid</code>中的数据变为一个数组，然后将整个表单的数据变为一个json String，<code>contentType</code>设置为  <code> &quot;application/json&quot;</code>,需要校验表单就在<code>ajax</code>的 <code>beforeSend</code>中调用。</p>
<p>后台我们使用<code>@RequestBody</code>来接受<code>json</code>类型的数据</p>
<figure class="highlight kotlin"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@RequestMapping(<span class="meta-string">&quot;save&quot;</span>)</span></span><br><span class="line">	<span class="meta">@ResponseBody</span></span><br><span class="line">	<span class="keyword">public</span> String save(<span class="meta">@RequestBody</span> User user, HttpServletRequest request) &#123;</span><br><span class="line">		System.<span class="keyword">out</span>.println(user.toString());</span><br><span class="line">		<span class="keyword">return</span> <span class="string">&quot;ztree&quot;</span>;</span><br><span class="line">	&#125;</span><br></pre></td></tr></table></figure>
<p>这样就可以直接填充<code>user</code>参数。</p>

        </div>
        
<blockquote class="copyright">
    <p><strong>本文链接 : </strong><a class="permalink" href="http://www.lushunming.com.cn/2017/01/02/web/2017-01-2-%E4%BD%BF%E7%94%A8spring%E7%9A%84requestBody%E5%AE%9E%E7%8E%B0List%E7%BB%91%E5%AE%9A/">http://www.lushunming.com.cn/2017/01/02/web/2017-01-2-使用spring的requestBody实现List绑定/</a></p>
    <p><strong>This article is available under <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank" rel="noopener noreferrer">Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.0)</a> License</strong></p>
</blockquote>


    </article>
    
    <section id="comments">
        

        
    </section>


    

</main>


<aside style="" id="sidebar" class="aside aside-fixture">
    <div class="toc-sidebar">
        <nav id="toc" class="article-toc">
            <h3 class="toc-title">文章目录</h3>
            <ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link"><span class="toc-number">1.</span> <span class="toc-text">使用spring的requestBody实现List绑定</span></a></li></ol>
        </nav>
    </div>
</aside>





        </section>
        <footer class="hidden lg:block fixed bottom-0 left-0 sm:w-1/12 lg:w-1/6 bg-gray-100 z-40">
    
    <div class="footer-social-links">
        
            <a target="_blank" rel="noopener" href="https://github.com/lushunming">
                <i class="iconfont icon-github"></i>
            </a>
        
            <a href="/atom.xml">
                <i class="iconfont icon-rss"></i>
            </a>
        
    </div>
    
    
</footer>

        <div id="mask" class="hidden mask fixed inset-0 bg-gray-900 opacity-75 z-40"></div>
        <div id="search-view-container" class="hidden shadow-xl"></div>
        
<script src="/js/dom-event.min.js"></script>

<script src="//cdn.jsdelivr.net/npm/yox@1.0.0-alpha.121/dist/standard/prod/yox.min.js"></script>


<script src="/js/search.min.js"></script>


    <script src="//cdn.jsdelivr.net/npm/gitalk@1.5.2/dist/gitalk.min.js"></script>
<script src="//cdn.jsdelivr.net/npm/blueimp-md5@2.10.0/js/md5.min.js"></script>
<script type="text/javascript">
    var gitalk = new Gitalk({
        clientID: 'd76d74420825181b7018',
        clientSecret: '8b9c3676695fa33d6b10d9697a96ef27c8301930',
        repo: 'blog-comment',
        owner: 'lushunming',
        admin: ['lushunming'],
        id: md5(location.pathname),
        distractionFreeMode: true
    })
    gitalk.render('comments')
</script>



    <script src="//cdn.jsdelivr.net/npm/lightgallery.js@1.1.3/dist/js/lightgallery.min.js"></script>
    
<script src="/js/light-gallery.min.js"></script>





    </body>
</html>
